﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    public class ETypeMailService : DbHelperBase
    {
        public int GetETypeMailListByIdCount(ETypeMailQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(*) AS [count] FROM UT_ETypeMail WHERE ETypeCode=@ETypeCode  ");
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ETypeCode", qcmodel.ETypeCode);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public IList<ETypeMailEntity> GetETypeMailListById(ETypeMailQcEntity qcmodel)
        {
            var list = new List<ETypeMailEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by InputTime) AS idx,RowID,ETypeCode,MailAddr FROM UT_ETypeMail ");
            strSql.Append(" WHERE ETypeCode=@ETypeCode ");
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ETypeCode", qcmodel.ETypeCode);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ETypeMailEntity();
                            if (reader["RowID"] != DBNull.Value)
                                model.RowID = new Guid(reader["RowID"].ToString());
                            model.MailAddr = reader["MailAddr"] == obj ? null : Convert.ToString(reader["MailAddr"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public bool AddETypeMail(List<ETypeMailEntity> list)
        {
            var result = false;
            var conn = Db.GetConnection() as SqlConnection;
            if (conn == null)
            {
                throw new InvalidOperationException("No Valid Database Connection!");
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            using (var bcp = new SqlBulkCopy(conn))
            {
                var mapRowId =
                    new SqlBulkCopyColumnMapping("RowID", "RowID");
                bcp.ColumnMappings.Add(mapRowId);

                var mapUserId =
                    new SqlBulkCopyColumnMapping("ETypeCode", "ETypeCode");
                bcp.ColumnMappings.Add(mapUserId);

                var mapLineId =
                    new SqlBulkCopyColumnMapping("MailAddr", "MailAddr");
                bcp.ColumnMappings.Add(mapLineId);

                bcp.DestinationTableName = "UT_ETypeMail";
                try
                {
                    bcp.WriteToServer(Collection.ToDataTable(list));
                    result = true;
                    conn.Close();
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool RemoveByRowId(string id)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_ETypeMail Where RowID=@RowID ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RowID", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool RemoveByETypeCode(string id)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_ETypeMail Where ETypeCode=@ETypeCode ");
            var result = false;
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ETypeCode", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public IList<ETypeMailEntity> GetETypeMailListByETypeCode(string id)
        {
            var list = new List<ETypeMailEntity>();
            var strSql = new StringBuilder();
            strSql.Append(" SELECT * FROM UT_ETypeMail WHERE ETypeCode=@ETypeCode order by InputTime ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@ETypeCode", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ETypeMailEntity();
                            model.RowID = ReaderHelper.GetValue<Guid>(reader, "RowID");
                            model.ETypeCode = ReaderHelper.GetValue<string>(reader, "ETypeCode");
                            model.MailAddr = ReaderHelper.GetValue<string>(reader, "MailAddr");
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }
    }
}
